@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

set /p input="请输入WIFI的名称(仅支持2.4Ghz): "
if "%input:~-1%"==" " set input=%input:~0,-1%
set /p =%input%<nul >>temp.txt
certutil -encode -f temp.txt encoded.txt

set lineFlag=0
for /f "delims=*" %%i in (encoded.txt) do (
set /a lineFlag+=1
  if !lineFlag!==2 (
    set ssid=%%i 
  )
)
if "%ssid:~-1%"==" " set ssid=%ssid:~0,-1%
echo 成功：wifi名加密为: !ssid!
del temp.txt
del encoded.txt

set /p input="请输入WIFI的密码: "
set /p =%input%<nul >>temp.txt
certutil -encode -f temp.txt encoded.txt


set lineFlag=0
for /f "delims=*" %%i in (encoded.txt) do (
set /a lineFlag+=1
  if !lineFlag!==2 (
    set wifipwd=%%i 
  )
)

if "%wifipwd:~-1%"==" " set wifipwd=%wifipwd:~0,-1%
echo 成功：wifi密码加密为: !wifipwd!
del temp.txt
del encoded.txt

set /p input="请输入拓竹账户的电话号码: "
set /p =%input%<nul >>temp.txt
certutil -encode -f temp.txt encoded.txt

set lineFlag=0
for /f "delims=*" %%i in (encoded.txt) do (
set /a lineFlag+=1
  if !lineFlag!==2 (
    set bblphone=%%i 
  )
)
if "%bblphone:~-1%"==" " set bblphone=%bblphone:~0,-1%
echo 成功：电话号码加密为: !bblphone!
del temp.txt
del encoded.txt

set /p input="请输入拓竹密码: "
set /p =%input%<nul >>temp.txt
certutil -encode -f temp.txt encoded.txt


set lineFlag=0
for /f "delims=*" %%i in (encoded.txt) do (
set /a lineFlag+=1
  if !lineFlag!==2 (
    set bblpwd=%%i 
    echo !bblpwd!
  )
)

if "%bblpwd:~-1%"==" " set bblpwd=%bblpwd:~0,-1%
echo 成功：拓竹密码加密为: !bblpwd!
del temp.txt
del encoded.txt

:: JSON内容
set "jsonContent={^"ssid^": ^"!ssid!^",^"pwd^": ^"!wifipwd!^",^"cloud-email^": ^"!bblphone!^",^"cloud-password^": ^"!bblpwd!^",^"cloud-region^": ^"China^"}"



:: 创建test.json文件
rmdir /S /q pack_dir
echo Folder "pack_dir" was deleted.
mkdir pack_dir
(
echo %jsonContent%
) > ./pack_dir/config.json

:: 完成提示
echo config.json文件已创建。


echo 请在设备管理器中查找esp32的端口号，请将esp32s3启动到刷机模式里
set /p portNumber=请输入想要刷入端口号（一个数字，COM后面的，不用加COM）: 


mkspiffs -c ./pack_dir -b 4096 -p 256 -s 0x1e0000 spiffs.bin

set "cmd=esptool.exe --chip esp32 --port COM%portNumber% --baud 460800 write_flash -z 0x210000 spiffs.bin"

echo 最终命令: %cmd%

%cmd%

pause
endlocal

